using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Zones;
using Framework.Library.Components;
using Framework.Library;
using Framework.Module.CMS.Library.Providers;

namespace Framework.Module.CMS.Library.Zones
{
    public class ZoneController
    {
        #region BackEnd
        private const string key = "Framework.Portal.CMS.Zone";
        public static DataTable GetZones(int parentId)
        {
            string param = "GetZones?" + parentId.ToString() + "&" + AppEnv.GetLanguage();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZones(parentId, AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }
        public static DataTable GetZones(int parentId, string portalid)
        {
            string param = "GetZones?" + parentId.ToString() + "&" + portalid + "&" + AppEnv.GetLanguage();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZones(parentId, portalid, AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }

        public static DataTable GetZones(int parentId, int portalid, int typezonevalue)
        {
            string param = "GetZones?" + parentId.ToString() + "&" + portalid.ToString() + "&" + typezonevalue.ToString() + "&" + AppEnv.GetLanguage();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZones(parentId, portalid, typezonevalue, AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }
        public static DataTable GetZonesDeployed(int contentId)
        {
            //string param = "GetZonesDeployed_" + contentId.ToString();
            //DataCaching data = new DataCaching();
            DataTable dtZones;//= (DataTable)data.GetHashCache(key, param);
            //if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZonesDeployed(contentId);
            //data.SetHashCache(key, param, Constants.CacheExpire_Zone, dtZones);
            return dtZones;
        }

        //GetZoneNameByZoneID
        public static string GetZoneNameByZoneID(int zoneid)
        {
            //string param = "GetZonesDeployed_" + contentId.ToString();
            //DataCaching data = new DataCaching();
            string zonename;//= (DataTable)data.GetHashCache(key, param);
            //if (dtZones != null) return dtZones;
            zonename = DataAccessProvider.Instance().GetZoneNameByZoneID(zoneid);
            //data.SetHashCache(key, param, Constants.CacheExpire_Zone, dtZones);
            return zonename;
        }

        public static ZoneInfo GetZone(int zoneId)
        {
            ZoneInfo zinfo = null;
            DataCaching dc = new DataCaching();
            zinfo = (ZoneInfo)dc.GetHashCache(key + zoneId, "");
            if (zinfo == null)
            {
                zinfo = DataAccessProvider.Instance().GetZone(zoneId);
                dc.SetHashCache(key + zoneId, "", 3, zinfo);
            }
            return zinfo;
        }
        public static void LoadZones(ListItemCollection lstItems)
        {
            DataTable dtZones = GetZones(0);
            foreach (DataRow row in dtZones.Rows)
            {
                ListItem item = new ListItem(row["Zone_Name"].ToString(), row["Zone_ID"].ToString());
                item.Attributes.Add("Level", "0");
                lstItems.Add(item);
                LoadForCurItem(lstItems, item);
            }
        }
        public static void LoadZones(ListItemCollection lstItems, string portalid)
        {
            DataTable dtZones = GetZones(0, portalid);
            foreach (DataRow row in dtZones.Rows)
            {
                ListItem item = new ListItem(row["Zone_Name"].ToString(), row["Zone_ID"].ToString());
                item.Attributes.Add("Level", "0");
                lstItems.Add(item);
                LoadForCurItem(lstItems, item);
            }
        }
        public static void LoadForCurItem(ListItemCollection lstItems, ListItem curItem)
        {
            int curId = ConvertUtility.ToInt32(curItem.Value);
            int level = ConvertUtility.ToInt32(curItem.Attributes["Level"]);
            level++;
            DataTable dtChildZones = GetZones(curId);
            foreach (DataRow row in dtChildZones.Rows)
            {
                ListItem item = new ListItem(MiscUtility.StringIndent(level) + row["Zone_Name"].ToString(), row["Zone_ID"].ToString());
                item.Attributes.Add("Level", level.ToString());
                lstItems.Add(item);
                LoadForCurItem(lstItems, item);
            }
        }
        public static void SetZonePriority(int zoneId, int priority)
        {
            DataAccessProvider.Instance().SetZonePriority(zoneId, priority);
            DataCaching data = new DataCaching();

        }
        public static int AddZone(ZoneInfo zone)
        {
            int id = DataAccessProvider.Instance().AddZone(zone);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
            return id;
        }
        public static int GetParentID(int zoneid)
        {
            int parentid = DataAccessProvider.Instance().GetParentID(zoneid);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
            return parentid;
        }
        public static void UpdateZone(ZoneInfo zone)
        {
            DataAccessProvider.Instance().UpdateZone(zone);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
        }
        public static void DeleteZone(int zoneId)
        {
            DataAccessProvider.Instance().DeleteZone(zoneId);
            DataCaching data = new DataCaching();
            data.RemoveCache(key);
        }

        #endregion

        #region FrontEnd
        public static DataTable GetZonesVisbleInNavbar()
        {
            string param = "GetZonesVisbleInNavbar?" + AppEnv.GetLanguage() + "&" + AppEnv.PortalId();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZonesVisbleInNavbar(AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }
        public static DataTable GetZonesVisbleInNavbar(int portalid)
        {
            string param = "GetZonesVisbleInNavbar?" + AppEnv.GetLanguage() + "&" + portalid.ToString();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZonesVisbleInNavbar(AppEnv.GetLanguage(), portalid);
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }
        public static DataTable GetZonesVisbleInTab()
        {
            string param = "GetZonesVisbleInTab?" + AppEnv.GetLanguage() + "&" + AppEnv.PortalId();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZonesVisbleInTab(AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }
        public static DataTable GetZonesVisbleInTop()
        {
            string param = "GetZonesVisbleInTop?" + AppEnv.GetLanguage() + "&" + AppEnv.PortalId();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZonesVisbleInTop(AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }

        public static DataTable GetZonesVisbleInRightBottom()
        {
            string param = "GetZonesVisbleInRightBottom?" + AppEnv.GetLanguage() + "&" + AppEnv.PortalId();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZonesVisbleInRightBottom(AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }

        public static DataTable GetZonesVisbleInLeftBottom()
        {
            string param = "GetZonesVisbleInLeftBottom?" + AppEnv.GetLanguage() + "&" + AppEnv.PortalId();
            DataCaching data = new DataCaching();
            DataTable dtZones = (DataTable)data.GetHashCache(key, param);
            if (dtZones != null) return dtZones;
            dtZones = DataAccessProvider.Instance().GetZonesVisbleInLeftBottom(AppEnv.GetLanguage());
            data.SetHashCache(key, param, AppEnv.CacheExpire_Zone, dtZones);
            return dtZones;
        }
        #endregion

        public static ZoneInfo ZoneInfo(int _iD)
        {
            ZoneInfo retVal = null;
            SqlConnection dbConn = new SqlConnection(AppEnv.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("CMS_SiteMap", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Zone_ID", _iD);
            try
            {
                dbConn.Open();
                SqlDataReader dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new ZoneInfo();
                    retVal.Zone_ID = ConvertUtility.ToInt32(dr["Zone_ID"]);
                    retVal.Zone_Name = ConvertUtility.ToString(dr["Zone_Name"]);
                    retVal.Zone_ParentID = ConvertUtility.ToInt32(dr["Zone_ParentID"]);
                    retVal.Portal_TabControlID = ConvertUtility.ToInt32(dr["Portal_TabControlID"]);
                }
                if (dr != null) dr.Close();
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
    }
}
